package com.youfan.control;

import com.youfan.entity.ProductattributeDict;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.ProductAttrDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "productAttr/productAttributeDict")
public class ProductAttributeDictControl {

    @Autowired
    private ProductAttrDictService productAttrDictService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestBody ProductattributeDict youfanproductattributeDict)
    {
        try {
            int sucessnums = productAttrDictService.insertOne(youfanproductattributeDict);
            return Result.success(sucessnums,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody ProductattributeDict youfanproductattributeDict){
        try {
            if (youfanproductattributeDict.getPageNum() == null) {
                youfanproductattributeDict.setPageNum(1);
            }
            if (youfanproductattributeDict.getPageSize() == null) {
                youfanproductattributeDict.setPageSize(5);
            }
            CommonPage<ProductattributeDict> resultdata = productAttrDictService.queryByCondtion(youfanproductattributeDict);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result view(@RequestBody  ProductattributeDict youfanproductattributeDict)
    {
        try {
            ProductattributeDict youfanproductattributeDict1 = productAttrDictService.findByid(youfanproductattributeDict);
            return Result.success(youfanproductattributeDict1,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestBody ProductattributeDict youfanproductattributeDict)
    {
        try {

            ProductattributeDict youfanproductattributeDict1 =  productAttrDictService.findByid(youfanproductattributeDict);
//            BeanUtils.copyProperties(youfanproductattributeDict,youfanproductattributeDict1);
            youfanproductattributeDict1.setCategorytype(youfanproductattributeDict.getCategorytype());
            youfanproductattributeDict1.setName(youfanproductattributeDict.getName());
            youfanproductattributeDict1.setInputType(youfanproductattributeDict.getInputType());
            youfanproductattributeDict1.setInputList(youfanproductattributeDict.getInputList());
            youfanproductattributeDict1.setSelectType(youfanproductattributeDict.getSelectType());
            youfanproductattributeDict1.setType(youfanproductattributeDict.getType());
            int result = productAttrDictService.updateOne(youfanproductattributeDict1);
            return Result.success(result,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


}
